/*-----------------------------------------------------
 * File: InParser.c
 *
 * Purpose:  Virtual Obj: Key Event Generator 
 *
 *
 * Description:  
 * 
 * Version: 0.01 a0
 *             
 * Copyright 2000 ArSilicii srl
 *-----------------------------------------------------*/

#include "hal.h"   /* Include HAL Definition */    
#include "InParser.h"

static enum    {Stable,Dump}   InParser_adump_sreg;
static enum    {Wait,Count}   InParser_sreg;
static union    uIP_Event LastValue;

struct tIP InParser;
       
void   InParser_Init(void) {
       InParser_adump_sreg=Stable;
       InParser_sreg=Wait;
       InParser.Value.all=0;
       InParser.Event.all=0;
}

void InParser_Ack(void)
{               
    
    InParser.Event.all= (InParser.Value.all ^ LastValue.all);
    LastValue.all=InParser.Value.all;
}

union uIP_Event * InParser_Descrambler(union uIP_Event *buf)
{               
    buf->all=0;
    buf->name.P1=KEY.P1;
    buf->name.P2=KEY.P2;
    return(buf);
}

void   InParser_Clk(void) {
    static unsigned char tCounter;
    static union uIP_Event DumpEvent,CumulEvent,SampleEvent;
      
    switch(InParser_adump_sreg) {
       case Stable:
          
          if(DumpEvent.all == (InParser_Descrambler(&SampleEvent)->all)){
                  InParser.Value.type.P = DumpEvent.type.P;
                  
          } else {
                  DumpEvent.all= SampleEvent.all;
                  InParser_adump_sreg=Dump;                  
          }                            
          break;
          
       case Dump:
          
          if(DumpEvent.all == (InParser_Descrambler(&SampleEvent)->all)){                  
                  InParser_adump_sreg=Stable;
                  
          } else {
                  DumpEvent.all= SampleEvent.all;                  
          } 
          break;          
    
    }
    
   
    
    switch(InParser_sreg) {
       case Wait:
          // Wait for low level event
          tCounter=0;          
          if(CumulEvent.type.P=InParser.Value.type.P)InParser_sreg=Count;  
          break;
                           
       case Count:
          
          CumulEvent.all &= (InParser.Value.all);
          
          if( tCounter++ == 32 ) {                 
                InParser.Value.type.S= CumulEvent.type.P;                
                
                if(CumulEvent.type.P == 0x03){
                    InParser.Value.name.OK=1;
                    InParser.Event.type.P=0x00;
                } else InParser.Value.name.OK=0;
                CumulEvent.all=0xff;
                tCounter=0;
          } 
          break;              
    }

}
